<template>
    <el-form ref="form" :model="form" label-width="120px"  class="form">
        <el-form-item label="关闭强更">
            <el-switch
                v-model="form.closeUpgrade"
                active-value="1"
                inactive-value="0">
            </el-switch>
        </el-form-item>
        <el-form-item label="关闭热更新">
            <el-switch
                v-model="form.closeHotUpdate"
                active-value="1"
                inactive-value="0">
            </el-switch>
        </el-form-item>
        <el-form-item label="关闭登录">
            <el-switch
                v-model="form.closeLogin"
                active-value="1"
                inactive-value="0">
            </el-switch>
        </el-form-item>
        <el-form-item label="关闭实名校验">
            <el-switch
                v-model="form.closeRealName"
                active-value="1"
                inactive-value="0">
            </el-switch>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit" :loading="loading">保存</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        data () {
            return {
                loading: false,
                form: {
                    closeUpgrade: '0',
                    closeHotUpdate: '0',
                    closeLogin: '0',
                    closeRealName: '0',
                },
            };
        },
        methods: {
            getData() {
                this.$store.dispatch('backend/QuickSet').then(res => {
                    this.form = this.$helper.deepClone(res.data.data);
                })
            },
            onSubmit() {
                this.$prompt('请输入操作密码', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /\w+/,
                    inputErrorMessage: '格式不正确',
                    beforeClose: (action, instance, done) => {
                        if (action !== 'confirm') this.getData();
                        done();
                    }
                }).then(({ value }) => {
                    let form = Object.assign({}, this.form, {
                        password: value,
                    })
                    this.loading = true;
                    this.$store.dispatch('backend/QuickSetUpdate', form).then(res => {
                        this.$message.success(res.data.message);
                    }).finally(() => {
                        this.loading = false;
                    });
                });
            }
        },
        created() {
            this.getData();
        }
    }
</script>

<style lang="scss" scoped>
.form {
    margin-top: 50px;
}
</style>
